#include "view_bridge.h"
#include "occt_widget.h"

#include <QVariant>

ViewBridge::ViewBridge()
{
    m_pQmlWindow = nullptr;
    m_pOcctWidget = nullptr;
}

ViewBridge::~ViewBridge()
{

}

void ViewBridge::resizeEmbeddedWidget(qreal _x, qreal _y, qreal _w, qreal _h)
{
    if(m_pOcctWidget) m_pOcctWidget->setGeometry(_x, _y, _w, _h);
}

void ViewBridge::readPly(int index, QString path)
{
    if(m_pOcctWidget) m_pOcctWidget->readPly(index, path);
}

void ViewBridge::readPlys(int index, QString path)
{
    if(m_pOcctWidget) m_pOcctWidget->readPlys(index, path);
}

void ViewBridge::removePlys(int index)
{
    if(m_pOcctWidget) m_pOcctWidget->removePlys(index);
}

void ViewBridge::readSTEP(QString path)
{
    if(m_pOcctWidget) m_pOcctWidget->readSTEP(path);
}

void ViewBridge::closeWindow()
{
    if(m_pOcctWidget) m_pOcctWidget->deleteLater();
}

void ViewBridge::unselectFace(int faceID)
{
    if(m_pOcctWidget) m_pOcctWidget->unselectFace(faceID);
}

void ViewBridge::setSTEPSlide(int value)
{

    if(m_pOcctWidget) m_pOcctWidget->setSTEPSlide(value);
}

void ViewBridge::registPly(bool isCoarse)
{
    if(m_pOcctWidget) m_pOcctWidget->registPly(isCoarse);
}

void ViewBridge::setQmlWindow(QWindow *_window)
{
    m_pQmlWindow = _window;
    m_pOcctWidget = new OcctWidget();
    WId parent_HWND = m_pQmlWindow->winId();
    m_pOcctWidget->setProperty("_q_embedded_native_parent_handle", QVariant(parent_HWND));
    m_pOcctWidget->winId();
    m_pOcctWidget->windowHandle()->setParent(m_pQmlWindow);
    m_pOcctWidget->initOcctWidget();
    m_pOcctWidget->show();
    connect(m_pOcctWidget, &OcctWidget::faceSelected, this, &ViewBridge::faceSelected);

    emit this->widgetReady();
}
